package com.yckj.activechicken.business.document.controller;

import com.yckj.activechicken.business.document.model.BusiDocument;
import com.yckj.activechicken.business.document.service.IBusiDocumentService;
import com.yckj.activechicken.common.utils.file.FileUtils;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.config.ProjectConfig;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【文档管理控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/15 17:19
 */
@RestController
@RequestMapping("/document")
public class BusiDocumentController extends BaseController {

    Logger log = LoggerFactory.getLogger(BusiDocumentController.class);

    @Autowired
    private IBusiDocumentService busiDocumentService;


    /**
     * 通过公司id以及文档类型查询文档信息
     * @param busiDocument
     * @return
     */
    @PreAuthorize("@ss.hasPermi('document:queryDocByCompanyId')")
    @PostMapping("/queryDocByCompanyId")
    public AjaxResult queryDocByCompanyId(@RequestBody BusiDocument busiDocument) {
        List<BusiDocument> list = busiDocumentService.queryDocByCompanyId(busiDocument);
        return  AjaxResult.success(list);
    }

    /**
     * 通过业务id查询文件信息
     * @param busiDocument
     * @return
     */
    @PreAuthorize("@ss.hasPermi('document:queryDocByBusiId')")
    @PostMapping("/queryDocByBusiId")
    public AjaxResult queryDocByBusiId(@RequestBody BusiDocument busiDocument) {
        List<BusiDocument> list = busiDocumentService.queryDocByBusiId(busiDocument);
        return  AjaxResult.success(list);
    }

    /**
     * 下载文件统一请求
     * @param response
     * @param request
     */
    @PreAuthorize("@ss.hasPermi('document:downloadDoc')")
    @GetMapping("/downloadDoc")
    public void downloadDoc(HttpServletResponse response, HttpServletRequest request) {
        try {

            String docName = request.getParameter("docName");
            String docUrl = request.getParameter("docUrl");
            String filePath = ProjectConfig.getProfile() + docUrl;

            response.setCharacterEncoding("utf-8");
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, docName));
            FileUtils.writeBytes(filePath, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 通过id删除文档记录，并删除对应文件
     * @param busiDocument
     * @return
     */
    @PreAuthorize("@ss.hasPermi('document:delDocById')")
    @RequestMapping("/delDocById")
    public AjaxResult delDocById(@RequestBody BusiDocument busiDocument) {
        return  toAjax(busiDocumentService.delDocById(busiDocument));
    }


    /**
     * 新增文档，存在多文件上传
     * @param busiDocument
     * @return
     */
    @PreAuthorize("@ss.hasPermi('document:addDocument')")
    @PostMapping("/addDocument")
    public AjaxResult addDocument(BusiDocument busiDocument) {
        String username = SecurityUtils.getUsername();
        busiDocument.setCreateBy(username);
        Date newDate = new Date();
        busiDocument.setCreateTime(newDate);
        busiDocument.setUpdateBy(username);
        busiDocument.setUpdateTime(newDate);
        Map<String,String> outMap = busiDocumentService.addDocument(busiDocument);
        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }


}
